package chars;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import javax.imageio.ImageIO;

public class platform {

    Image appearance;
    public int x;
    public int y;
    int falling = 10;
    int width;
    int height;

    public platform(int x, int y, int which) {
        this.x = x;
        this.y = y;
        try {
            if (which == 1) {
                appearance = ImageIO.read(new File("Images/platforms/base.jpg"));
            }
            if (which == 2) {
                appearance = ImageIO.read(new File("Images/platforms/bottom.jpg"));
            }
            if (which == 3) {
                appearance = ImageIO.read(new File("Images/platforms/top.png"));
            }
            if (which == 4) {
                appearance = ImageIO.read(new File("Images/platforms/mid.jpg"));
            }
            if (which == 5) {
                appearance = ImageIO.read(new File("Images/platforms/basemid.jpg"));
            }
            if (which == 6) {
                appearance = ImageIO.read(new File("Images/platforms/baseright.jpg"));
            }
            if (which == 7) {
                appearance = ImageIO.read(new File("Images/platforms/baseleft.jpg"));
            }
            if (which == 8) {
                appearance = ImageIO.read(new File("Images/platforms/midleft.jpg"));
            }
            if (which == 9) {
                appearance = ImageIO.read(new File("Images/platforms/midright.jpg"));
            }
            if (which == 10) {
                appearance = ImageIO.read(new File("Images/platforms/midmid.jpg"));
            }
            if (which == 11) {
                appearance = ImageIO.read(new File("Images/platforms/topmid.jpg"));
            }
            if (which == 12) {
                appearance = ImageIO.read(new File("Images/platforms/topleft.jpg"));
            }
            if (which == 13) {
                appearance = ImageIO.read(new File("Images/platforms/topright.jpg"));
            }
            if (which == 14) {
                appearance = ImageIO.read(new File("Images/platforms/bottommid.jpg"));
            }
            if (which == 15) {
                appearance = ImageIO.read(new File("Images/platforms/bottomleft.png"));
            }
            if (which == 16) {
                appearance = ImageIO.read(new File("Images/platforms/bottomright.png"));
            }
            width = appearance.getWidth(null);
            height = appearance.getHeight(null);
        } catch (java.io.IOException e) {
            System.err.println(e.getMessage());
        }
    }

    public boolean draw(Graphics g, platformer.Panel p) {
        if (x + appearance.getWidth(null) > 0 && x < 600) {
            g.drawImage(appearance, x, y, appearance.getWidth(null), appearance.getHeight(null), p);
            return true;
        }
        return false;
    }

    public void checkCollision(Player player) {
        int pWidth = player.walk1.getWidth(null);
        int pHeight = player.walk1.getHeight(null);
        boolean sideBound = false;
        if (player.x > this.x - pWidth && player.x < this.x + width) {
            if (player.y + pHeight > this.y + 3 && player.y < this.y + height) { //bounce off sides
                if (player.x + pWidth < this.x + 7 && player.x + pWidth > this.x) { //left side
                    player.x = this.x - pWidth - 1;
                    player.xSpeed = -.5;
                    sideBound = true;
                } else {
                    if (player.x > this.x + width - 3 && player.x < this.x + width) { //right side
                        player.x = this.x + width;
                        player.xSpeed = .5;
                        sideBound = true;
                    }
                }
            }
            if (player.y + pHeight <= this.y + 3 && player.y + pHeight >= this.y && player.ySpeed > 0) { //land on top
                player.y = this.y - pHeight;
                player.ySpeed = 0;
                player.jumping = false;
                falling = 2;
            }
            if (player.y <= this.y + height && player.y >= this.y + height + player.ySpeed && player.ySpeed < 0 && !sideBound) { //bounce off bottom
                player.y = this.y + height;
                player.ySpeed = 1;
            }
            falling--;
            if (falling == 0) {
                player.jumping = true;
            }
        }
    }
}
